package com.wobangkj.common.repository;

import com.wobangkj.common.model.crud.Dynamic;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public interface DynamicRepository extends JpaRepository<Dynamic, Long> {


    @Query(nativeQuery = true,value = "select s.*,u.major,u.name,u.mst_company_id,u.mst_job,u.grade,u.school,u.college,u.headimg from dynamic s inner join user u on s.dynamic_publisher_id = u.id group by s.id;") //原生SQL方法
    List<Map> list();

    @Query(nativeQuery = true,value = "select e.*,c.circle_name,c.circle_headimg from (select s.*,u.major,u.name,u.mst_company_id,u.mst_job,u.grade,u.school,u.college,u.headimg from dynamic s inner join user u on s.dynamic_publisher_id = u.id group by s.id) e inner join circle c on e.dynamic_circle_id = c.id  where c.circle_name= :name ;") //原生SQL方法
    List<Map> getListByName(String name);

    @Query(nativeQuery = true,value = "select e.*,c.circle_name,c.circle_headimg from (select s.*,u.major,u.name,u.mst_company_id,u.mst_job,u.grade,u.school,u.college,u.headimg from dynamic s inner join user u on s.dynamic_publisher_id = u.id group by s.id) e inner join circle c on e.dynamic_circle_id = c.id  where c.circle_name= :circle_name " +
            "and (e.dynamic_company_tag=:dynamic_company_tag or :dynamic_company_tag=0 )and ( e.dynamic_major_tag=:dynamic_major_tag or :dynamic_major_tag=0 ) and (e.dynamic_job_tag=:dynamic_job_tag or :dynamic_job_tag=0 ) and (e.dynamic_other_tag=:dynamic_other_tag  or :dynamic_other_tag=0 ) ;") //原生SQL方法
    List<Map> getListByTag(String circle_name,String dynamic_company_tag,String dynamic_major_tag,String dynamic_job_tag,String dynamic_other_tag) ;

    @Query(nativeQuery = true,value = "select s.*,u.major,u.name,u.mst_company_id,u.mst_job,u.grade,u.school,u.college,u.headimg from " +
            "dynamic s inner join user u on s.dynamic_publisher_id = u.id   where " +
            "(s.dynamic_company_tag=:dynamic_company_tag or :dynamic_company_tag=0 )and ( s.dynamic_major_tag=:dynamic_major_tag or :dynamic_major_tag=0 ) " +
            "and (s.dynamic_job_tag=:dynamic_job_tag or :dynamic_job_tag=0 ) and (s.dynamic_other_tag=:dynamic_other_tag  or :dynamic_other_tag=0 ) ;") //原生SQL方法
    List<Map> getListByTagNoCircle(String dynamic_company_tag,String dynamic_major_tag,String dynamic_job_tag,String dynamic_other_tag) ;

    @Query(nativeQuery = true,value = "select s.*,u.major,u.name,u.mst_company_id,u.mst_job,u.grade,u.school,u.college,u.headimg from dynamic s inner join user u on s.dynamic_publisher_id = u.id where s.id= :id") //原生SQL方法
    List<Map> idlist(long id);

    @Query(nativeQuery = true, value="update dynamic set dynamic_forward_num=dynamic_forward_num+1 where id=:id  ")
    @Modifying
    public void updateDynamicForward(Long id);

}
